/*
 * Copyright (C) 2011, EADS France
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 */
package xowl.gmi.view;

import java.nio.charset.Charset;
import xowl.gmi.environment.Locale;
import xowl.gmi.environment.Properties;
import xowl.utils.config.Configuration;

/**
 *
 * @author Laurent WOUTERS
 */
public class DialogAboutBox extends javax.swing.JDialog {
    private static int width = 500;
    private static int height = 300;
    private static int border = 10;
    
    public DialogAboutBox(Properties properties, Locale locale, javax.swing.JFrame mainFrame) {
        super(mainFrame);
        java.awt.Rectangle bounds = mainFrame.getBounds();
        int x = bounds.x + (bounds.width-width)/2;
        int y = bounds.y + (bounds.height-height)/2;
        this.setBounds(x, y, width, height);
        this.setModalityType(javax.swing.JDialog.ModalityType.APPLICATION_MODAL);
        this.setResizable(false);
        this.setTitle(locale.aboutAbout + " " + properties.title);
        this.setIconImage(IconProvider.getEmbedded(locale.aboutMenuItem_icon).getImage());
        
        Configuration authors = new Configuration();
        try {
            java.lang.ClassLoader loader = java.lang.ClassLoader.getSystemClassLoader();
            java.io.InputStream stream = loader.getResourceAsStream("xowl/gmi/resources/authors.ini");
            authors.load(stream, Charset.forName("UTF-8"));
        } catch (java.io.IOException ex) { }
        
        javax.swing.JTextPane description = new javax.swing.JTextPane();
        write(description, IconProvider.getEmbedded("eadsiw.png"));
        write(description, System.lineSeparator(), 18, false);
        write(description, "xOWL Infrastructure" + System.lineSeparator(), 18, false);
        write(description, locale.aboutApplication + " ", true);
        write(description, properties.title + System.lineSeparator());
        write(description, locale.aboutVersion + " ", true);
        write(description, properties.version + System.lineSeparator());
        write(description, locale.aboutCopyright + " ", true);
        write(description, properties.copyright + System.lineSeparator());
        write(description, locale.aboutLicence + " ", true);
        write(description, properties.licence + System.lineSeparator());
        write(description, locale.aboutHomepage + " ", true);
        write(description, properties.homepage + System.lineSeparator());
        write(description, locale.aboutDescription + " ", true);
        write(description, properties.description + System.lineSeparator());
        write(description, System.lineSeparator());
        write(description, System.lineSeparator());
        write(description, locale.aboutThirdParty + System.lineSeparator(), 18, false);
        write(description, "Parser Generator:" + System.lineSeparator(), true);
        write(description, "Hime Parser Generator 0.4.0. GNU Lesser General Public Licence v2.1." + System.lineSeparator());
        write(description, "See http://himeparser.codeplex.com/" + System.lineSeparator());
        write(description, "Icons:" + System.lineSeparator(), true);
        write(description, "Some icons by Yusuke Kamiyamane (Fugue 3.3.4). All rights reserved. Creative Commons Attribution 3.0 Unported License." + System.lineSeparator());
        write(description, "See http://p.yusukekamiyamane.com/" + System.lineSeparator());
        write(description, "Docking Framework:" + System.lineSeparator(), true);
        write(description, "Mydoggy 1.5.0 (Beta 1). GNU Lesser General Public Licence v3." + System.lineSeparator());
        write(description, "See http://mydoggy.sourceforge.net/index.html/" + System.lineSeparator());
        write(description, System.lineSeparator());
        write(description, System.lineSeparator());
        write(description, locale.aboutAuthors + System.lineSeparator(), 18, false);
        for (String author : authors.getValues("author"))
            write(description, author + System.lineSeparator());
        description.setEditable(false);
        
        javax.swing.JScrollPane scroll = new javax.swing.JScrollPane(description);
        
        javax.swing.JPanel column = new javax.swing.JPanel();
        column.setOpaque(false);
        column.setLayout(new javax.swing.BoxLayout(column, javax.swing.BoxLayout.Y_AXIS));
        column.add(javax.swing.Box.createRigidArea(new java.awt.Dimension(0, border)));
        column.add(scroll);
        column.add(javax.swing.Box.createRigidArea(new java.awt.Dimension(0, border)));
        
        javax.swing.JPanel row = new javax.swing.JPanel();
        row.setOpaque(false);
        row.setLayout(new javax.swing.BoxLayout(row, javax.swing.BoxLayout.X_AXIS));
        row.add(javax.swing.Box.createRigidArea(new java.awt.Dimension(10, 0)));
        row.add(column, java.awt.Component.CENTER_ALIGNMENT);
        row.add(javax.swing.Box.createRigidArea(new java.awt.Dimension(10, 0)));

        javax.swing.JPanel pane = (javax.swing.JPanel)getContentPane();
        pane.setOpaque(false);
        pane.setLayout(new java.awt.BorderLayout());
        pane.add(row, java.awt.BorderLayout.CENTER);
    }
    
    private void write(javax.swing.JTextPane pane, javax.swing.Icon icon) {
        try {
            javax.swing.text.SimpleAttributeSet attributes = new javax.swing.text.SimpleAttributeSet();
            javax.swing.text.StyleConstants.setIcon(attributes, icon);
            pane.getStyledDocument().insertString(pane.getStyledDocument().getLength(), "image", attributes);
        } catch (javax.swing.text.BadLocationException ex) { }
    }
    private void write(javax.swing.JTextPane pane, String text) {
        try {
            javax.swing.text.SimpleAttributeSet attributes = new javax.swing.text.SimpleAttributeSet();
            javax.swing.text.StyleConstants.setFontFamily(attributes, "Sans-serif");
            javax.swing.text.StyleConstants.setFontSize(attributes, 12);
            pane.getStyledDocument().insertString(pane.getStyledDocument().getLength(), text, attributes);
        } catch (javax.swing.text.BadLocationException ex) { }
    }
    private void write(javax.swing.JTextPane pane, String text, boolean bold) {
        try {
            javax.swing.text.SimpleAttributeSet attributes = new javax.swing.text.SimpleAttributeSet();
            javax.swing.text.StyleConstants.setFontFamily(attributes, "Sans-serif");
            javax.swing.text.StyleConstants.setFontSize(attributes, 12);
            javax.swing.text.StyleConstants.setBold(attributes, bold);
            pane.getStyledDocument().insertString(pane.getStyledDocument().getLength(), text, attributes);
        } catch (javax.swing.text.BadLocationException ex) { }
    }
    private void write(javax.swing.JTextPane pane, String text, int size, boolean bold) {
        try {
            javax.swing.text.SimpleAttributeSet attributes = new javax.swing.text.SimpleAttributeSet();
            javax.swing.text.StyleConstants.setFontFamily(attributes, "Sans-serif");
            javax.swing.text.StyleConstants.setFontSize(attributes, size);
            javax.swing.text.StyleConstants.setBold(attributes, bold);
            pane.getStyledDocument().insertString(pane.getStyledDocument().getLength(), text, attributes);
        } catch (javax.swing.text.BadLocationException ex) { }
    }
}
